/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.huawei.spare.part.replace.views.replace.slot;

import com.huawei.spare.part.replace.framework.SmartPresenter;
import com.huawei.spare.part.replace.models.Replacement;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.net.URL;

import static com.huawei.spare.part.replace.Constants.*;

/**
 * @author Woo
 */
@Getter
public class SlotItemPresenter extends SmartPresenter {

    @FXML VBox slotItem;
    @FXML Label slotId;
    @FXML RadioButton selected;
    @FXML Pane slotImagePane;

    @Override
    public void initialize() {
        slotItem.setOnMouseClicked((e) -> {
            selected.setSelected(true);
            selected.requestFocus();
        });
    }

    public void render(Replacement.Slot slot) {
        // set slot id
        this.slotId.setText(SlotNames.get(slot.getPos() - 1));
        this.selected.setUserData(slot);
        if (slot.isPresent()) { // 如果未使用，需要显示图片？
            // update switch image
            final String imageUrl = String.format("/assets/e9000/%s.png", StringUtils.defaultIfBlank(slot.getBoardType(), "NA"));
            final URL resource = SlotPresenter.class.getResource(imageUrl);
            String resourceUrl = resource != null ? resource.toExternalForm().toString() : "/assets/e9000/NA.png";
            Image image = new Image(resourceUrl);
            ImageView slotImage = new ImageView(image);
            slotImage.setFitHeight(SwitchModelHeight);
            slotImage.setFitWidth(SwitchModelWidth);
            this.slotImagePane.getChildren().add(slotImage);
        }

        // set anchor
        AnchorPane.setTopAnchor(slotItem, E9000SlotTopAt - E9000SlotItemLabelHeight);
        final double anchorRight = E9000ModelWidth - SlotRightPos.get(slot.getPos() - 1);
        AnchorPane.setRightAnchor(slotItem, anchorRight);
    }
}
